import { _decorator, Canvas, Component, director, instantiate, Node } from 'cc';
import { aaEvent, ui, uiInterface } from '../common/Enums';
import resMgr from './ResMgr';
import { promptConfig, PromptView } from '../../view/PromptView';
import globalVarient from '../common/Global';


class UiMgr extends Component {

    /**
     * @en : get ui from loaded resources and put it into scene
     * @zh : 加载ui到场景
     * @param {uiInterface} Path
     * @param {Node} Parent
     */
    public async get(Path:uiInterface, Parent?: Node) {
        const layer2D = globalVarient.layers;

        if (Path.clear) {
            if (!Parent && layer2D[Path.layer].children[0]) {
                if (layer2D[Path.layer].children[0].name == Path.name) return
                resMgr.put(layer2D[Path.layer].children[0])
            }
        }
        let ParentNode = Parent ? Parent : layer2D[Path.layer]
        return await resMgr.create(Path, ParentNode)
    }
    /**
     * @en : remove UI from scene
     * @zh : 从场景中移除UI
     * @param {uiInterface} Path
     */
    public put(Path:uiInterface){
        const node =globalVarient.layers[Path.layer].getChildByName(Path.name);
        if(node){
            resMgr.put(node);
        }
    }

    public toast(content:string, time=2){
        director.emit(aaEvent.toast, content, time);
    }

    public async prompt(config:promptConfig){
        const prompt = await this.get(ui.PromptView);
        prompt.getComponent(PromptView).init(config);
    }

    public isLoading(time?:number){
        this.get(ui.WaitView);
        if(time && time >0){
            this.scheduleOnce(() => {
                this.put(ui.WaitView);
            }, time)
        }
    }
    public putLoading(){
        this.put(ui.WaitView);
    }
   

}
const uiMgr = new UiMgr;
export default uiMgr;
